package com.tsinghuabigdata.edu.ddmath.module.onlineTutoring.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;

import com.tsinghuabigdata.edu.ddmath.R;
import com.tsinghuabigdata.edu.ddmath.bean.DayQuestions;
import com.tsinghuabigdata.edu.ddmath.bean.Question;
import com.tsinghuabigdata.edu.ddmath.module.onlineTutoring.bean.ImagesBean;
import com.tsinghuabigdata.edu.ddmath.module.onlineTutoring.bean.UploadImage;
import com.tsinghuabigdata.edu.ddmath.util.GlobalData;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static android.R.id.list;


/**
 * 在线辅导——选择题目适配器
 * Created by Administrator on 2017/3/3.
 */

public class SelectQuestionsBaseAdapter extends BaseAdapter {

    private Context           mContext;
    private List<UploadImage> mDatas;
    private List<HashMap<Integer, Boolean>> mMapList = new ArrayList<HashMap<Integer, Boolean>>();

    public SelectQuestionsBaseAdapter(Context context, List<UploadImage> list) {
        mDatas = list;
        mContext = context;
        HashMap<String, Boolean> sTeamMap = GlobalData.getTeamMap();
        for (int i = 0; i < list.size(); i++) {
            UploadImage uploadImage = list.get(i);
            List<ImagesBean> imagesBeanList = uploadImage.getImages();
            HashMap<Integer, Boolean> hashMap = new HashMap<Integer, Boolean>();
            for (int j = 0; j < imagesBeanList.size(); j++) {
                ImagesBean imagesBean = imagesBeanList.get(j);
                if (sTeamMap.get(imagesBean.getImageId()) != null) {
                    hashMap.put(j, true);
                }
            }
            mMapList.add(hashMap);
        }
    }


    @Override
    public int getCount() {
        if (mDatas != null) {
            return mDatas.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (mDatas != null) {
            return mDatas.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Log.i("sky", "getView position=" + position);
        ViewHolder holder = null;
        if (convertView == null) {
            // 没有复用
            convertView = View.inflate(mContext, R.layout.item_select_questions, null);
            holder = new ViewHolder();
            convertView.setTag(holder);
            // 给holder找view
            holder.cb = (CheckBox) convertView.findViewById(R.id.cb_date);
            holder.rv = (RecyclerView) convertView.findViewById(R.id.rv_day_questions);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        UploadImage vo = mDatas.get(position);
        holder.cb.setText(vo.getDate());
        HashMap<Integer, Boolean> hashMap = mMapList.get(position);
        if (hashMap.keySet().size() == vo.getImages().size()){
            holder.cb.setChecked(true);
        }else {
            holder.cb.setChecked(false);
        }

        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        holder.rv.setLayoutManager(layoutManager);
        List<ImagesBean> teamplist = new ArrayList<ImagesBean>();
        teamplist.addAll(vo.getImages());

        DayQuestionsAdapter adapter;
        if (holder.rv.getAdapter() == null) {
            Log.i("sky", "getAdapter()==null");
            adapter = new DayQuestionsAdapter(mContext, teamplist);
            adapter.setMap(hashMap);
            holder.rv.setAdapter(adapter);
        } else {
            Log.i("sky", "getAdapter()!=null   list.size()=" + teamplist.size());
            adapter = (DayQuestionsAdapter) holder.rv.getAdapter();
            adapter.setData(teamplist);
            adapter.setMap(hashMap);
            adapter.notifyDataSetChanged();
        }

        final ViewHolder finalHolder = holder;
        adapter.setQuestionSelectListener(new DayQuestionsAdapter.QuestionSelectListener() {
            @Override
            public void select(String questionId, boolean allSelect) {
                finalHolder.cb.setChecked(allSelect);
            }
        });
        final ViewHolder finalHolder1 = holder;
        holder.cb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckBox checkBox = (CheckBox) v;
                if (checkBox.isChecked()) {
                    chooseAll(finalHolder1.rv.getAdapter(), position, true);
                } else {
                    chooseAll(finalHolder1.rv.getAdapter(), position, false);
                }
            }
        });

        return convertView;
    }

    private void chooseAll(RecyclerView.Adapter adapter, int position, boolean all) {
        HashMap<Integer, Boolean> hashMap = mMapList.get(position);
        UploadImage uploadImage = mDatas.get(position);
        int size = uploadImage.getImages().size();
        for (int j = 0; j < size; j++) {
            ImagesBean vo = uploadImage.getImages().get(j);
            if (all) {
                hashMap.put(j, true);
                GlobalData.getTeamMap().put(vo.getImageId(), true);
            } else {
                hashMap.remove(j);
                GlobalData.getTeamMap().remove(vo.getImageId());
            }

        }
        UploadImage vo = mDatas.get(position);
        List<ImagesBean> teamplist = new ArrayList<ImagesBean>();
        teamplist.addAll(vo.getImages());
        DayQuestionsAdapter dayQuestionsAdapter = (DayQuestionsAdapter) adapter;
        dayQuestionsAdapter.setData(teamplist);
        dayQuestionsAdapter.setMap(mMapList.get(position));
        adapter.notifyDataSetChanged();
    }

    public void addMap() {
        HashMap<String, Boolean> sTeamMap = GlobalData.getTeamMap();
        for (int i = mMapList.size(); i < mDatas.size(); i++) {
            UploadImage uploadImage = mDatas.get(i);
            List<ImagesBean> imagesBeanList = uploadImage.getImages();
            HashMap<Integer, Boolean> hashMap = new HashMap<Integer, Boolean>();
            for (int j = 0; j < imagesBeanList.size(); j++) {
                ImagesBean imagesBean = imagesBeanList.get(j);
                if (sTeamMap.get(imagesBean.getImageId()) != null) {
                    hashMap.put(j, true);
                }
            }
            mMapList.add(hashMap);
        }
    }

    class ViewHolder {
        CheckBox     cb;
        RecyclerView rv;
    }

}

